﻿using OxyPlot;
using OxyPlot.Axes;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using System.Windows.Threading;

namespace WpfApplication1.Controllers
{
    public class MainController:IController
    {
        private int i, j = 0;
        private ViewModels.MainViewModel mainViewModel;
        private Models.Aircraft aircraft;

        public Connections.UdpServer udpServer { get; private set; }
        public bool MODULATING_PAUSED { get; set; }
        public MainController(ViewModels.MainViewModel mainViewModel)
        {
            this.mainViewModel = mainViewModel;
            udpServer = new Connections.UdpServer(this);
            udpServer.startUdpClients();
            aircraft = new Models.Aircraft();
        }


        public void addNewPoint()
        {
            mainViewModel.altitudeLineSeries.Points.Add(new DataPoint(i, i));
            mainViewModel.altitudePlotModel.RefreshPlot(true);
            i++;
            j += 5;
            Console.WriteLine("addnewpoint");
        }
        //called from udpserver
        public String processUdpMessage(String message, double samplingTime)
        {
            UdpMessageEncryption.decodeUdpMessage(aircraft, message);
            //aircraft.writeAllParametersToConsole();
            App.Current.Dispatcher.BeginInvoke(new Action(plotModels));
            return "";
        }

        public void plotModels()
        {
            plotAltitude();
            plotSpeed();
        }

        public void plotAltitude()
        {
            mainViewModel.altitudeLineSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), aircraft.altitude));
            mainViewModel.altitudePlotModel.RefreshPlot(true);
            //Console.WriteLine("Points:" + mainViewModel.altitudeLineSeries.Points.Count);
        }
        public void plotSpeed()
        {
            mainViewModel.speedLineSeries.Points.Add(new DataPoint(DateTimeAxis.ToDouble(DateTime.Now), aircraft.speed));
            mainViewModel.speedPlotModel.RefreshPlot(true);
            //Console.WriteLine("Points:" + mainViewModel.altitudeLineSeries.Points.Count);
        }

    }
}
